home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / block.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-18  |  5.1 KB  |  207 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Note: This file was modified by Crystal Decisions in June 2002.
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. #ifndef _BLOCK_H_
  29. #define _BLOCK_H_
  30.  
  31. #include "config.h"
  32.  
  33. #include <assert.h>
  34.  
  35.  
  36. class superblock;
  37.  
  38. class block {
  39. public:
  40.  
  41.   block (superblock * sb)
  42.     : 
  43. #if HEAP_DEBUG
  44.       _magic (FREE_BLOCK_MAGIC),
  45. #endif
  46.       _next (NULL)
  47. #if !defined(CRYSTAL_HOARD) || !defined(WIN32)
  48.       ,_mySuperblock (sb)
  49. #endif
  50.       {}
  51.  
  52.   block& operator= (const block& b) {
  53. #if HEAP_DEBUG
  54.     _magic = b._magic;
  55. #endif
  56.     _next = b._next;
  57. #if !defined(CRYSTAL_HOARD) || !defined(WIN32)
  58.     _mySuperblock = b._mySuperblock;
  59. #endif
  60. #if HEAP_FRAG_STATS
  61.     _requestedSize = b._requestedSize;
  62. #endif
  63.     return *this;
  64.   }
  65.  
  66.   enum { ALLOCATED_BLOCK_MAGIC = 0xcafecafe,
  67.      FREE_BLOCK_MAGIC = 0xbabebabe };
  68.  
  69.   // Mark this block as free.
  70.   inline void markFree (void);
  71.  
  72.   // Mark this block as allocated.
  73.   inline void markAllocated (void);
  74.  
  75.   // Is this block valid? (i.e.,
  76.   // does it have the right magic number?)
  77.   inline const int isValid (void) const;
  78.  
  79.   // Return the block's superblock pointer.
  80.   inline superblock * getSuperblock (void);
  81.  
  82. #if HEAP_FRAG_STATS
  83.   void setRequestedSize (size_t s) 
  84.   {   
  85.     _requestedSize = s;
  86.   }
  87.  
  88.   size_t getRequestedSize (void) { return _requestedSize; }
  89. #endif
  90.  
  91. // The Win32 version of Crystal Hoard needs to know the actual size of
  92. // the block, to support Microsoft's _msize() extension to the CRT.
  93. #if USE_PRIVATE_HEAPS || ( defined(CRYSTAL_HOARD) && defined(WIN32) )
  94.   void setActualSize (size_t s) { _actualSize = s; }
  95.   size_t getActualSize (void) { return _actualSize; }
  96. #endif
  97.  
  98.   void setNext (block * b) { _next = b; }
  99.   block * getNext (void) { return _next; }
  100.  
  101. private:
  102.  
  103. #if USE_PRIVATE_HEAPS
  104.  
  105. #if HEAP_DEBUG
  106.   union {
  107.     unsigned long _magic;
  108.     double _d1; // For alignment.
  109.   };
  110. #endif
  111.  
  112.   block * _next;    // The next block in a linked-list of blocks.
  113.   size_t _actualSize;    // The actual size of the block.
  114.  
  115. #if !defined(CRYSTAL_HOARD) || !defined(WIN32)
  116.   union {
  117.     double _d2; // For alignment.
  118.     superblock *    _mySuperblock;    // A pointer to my superblock.
  119.   };
  120. #endif
  121.  
  122.  
  123. #else // ! USE_PRIVATE_HEAPS
  124.  
  125. #if HEAP_DEBUG
  126.   union {
  127.     unsigned long _magic;
  128.     double _d3; // For alignment.
  129.   };
  130. #endif
  131.  
  132.   block *    _next;        // The next block in a linked-list of blocks.
  133.  
  134.   // The Win32 version of Crystal Hoard needs to know the actual size of
  135.   // the block, to support Microsoft's _msize() extension to the CRT.
  136.   // Also, the Win32 version of CrystalHoard always allocates 64k superblocks
  137.   // aligned on 64k boundaries, so we don't need a superblock pointer -- we
  138.   // can just use a mask to find the superblock.
  139. #if defined(CRYSTAL_HOARD) && defined(WIN32)
  140.   size_t _actualSize;   // The amount of space requested (vs. allocated).
  141. #else
  142.   superblock *    _mySuperblock;    // A pointer to my superblock.
  143. #endif
  144.  
  145.  
  146. #endif // USE_PRIVATE_HEAPS
  147.  
  148. #if HEAP_FRAG_STATS
  149.   union {
  150.     double _d4; // This is just for alignment purposes.
  151.     size_t _requestedSize;    // The amount of space requested (vs. allocated).
  152.   };
  153. #endif
  154.  
  155.   // Disable copying.
  156.  
  157.   block (const block&);
  158. };
  159.  
  160.  
  161. superblock * block::getSuperblock (void)
  162. {
  163. #if HEAP_DEBUG
  164.   assert (isValid());
  165. #endif
  166.  
  167. // The Win32 version of CrystalHoard always allocates 64k superblocks
  168. // aligned on 64k boundaries, so we don't need a superblock pointer -- we
  169. // can just use a mask to find the superblock.
  170. #if defined(CRYSTAL_HOARD) && defined(WIN32)
  171.   return( ( superblock * )( ( unsigned long )this & 0xFFFF0000 ) );
  172. #else
  173.   return _mySuperblock;
  174. #endif
  175. }
  176.  
  177.  
  178. void block::markFree (void)
  179. {
  180. #if HEAP_DEBUG
  181.   assert (_magic == ALLOCATED_BLOCK_MAGIC);
  182.   _magic = FREE_BLOCK_MAGIC;
  183. #endif
  184. }
  185.  
  186.  
  187. void block::markAllocated (void)
  188. {
  189. #if HEAP_DEBUG
  190.   assert (_magic == FREE_BLOCK_MAGIC);
  191.   _magic = ALLOCATED_BLOCK_MAGIC;
  192. #endif
  193. }
  194.  
  195.  
  196. const int block::isValid (void) const 
  197. {
  198. #if HEAP_DEBUG
  199.   return ((_magic == FREE_BLOCK_MAGIC)
  200.       || (_magic == ALLOCATED_BLOCK_MAGIC));
  201. #else
  202.   return 1;
  203. #endif
  204. }
  205.  
  206. #endif // _BLOCK_H_
  207.